<?php
namespace app\api\validate;
use think\Validate;
use app\common\tool\Enum;

class Task extends Validate
{
    protected $rule = [
        'name'                  =>  'require',
        'tasklist'              =>  'require|checkTasklist',
        'finish_date'           =>  'require|date',
        'important'             =>  'require|in:0,1,2,3',
        'brand_id'              =>  'require|number',
        'product_id'            =>  'require|number',
        'charge_userid'         =>  'require',
        'charge_departmentId'   =>  'require',
        'filelist'              =>  'checkFilelist',
        'type'                  =>  'require|in:baseinfo,loglist',
        'tid'                   =>  'require',
        'method'                =>  'require|checkMethodParams',
    ];

    protected $message = [
        'name.require'                  => '需求名称不能为空!',
        'tasklist.require'              => '需求清单不能为空!',
        'finish_date.require'           => '期望日期不能为空!',
        'finish_date.date'              => '期望日期格式错误!',
        'important.require'             => '优先级不能为空!',
        'important.in'                  => '优先级参数错误!',
        'brand_id.require'              => '品牌ID不能为空!',
        'brand_id.number'               => '品牌ID参数错误!',
        'product_id.require'            => '产品ID不能为空!',
        'product_id.number'             => '产品ID参数错误!',
        'charge_userid.require'         => '负责人不能为空!',
        'charge_departmentId.require'   => '负责人部门ID不能为空!',
        'tid.require'                   => '任务ID不能为空!',
        'type.require'                  => '查看类型不能为空!',
        'type.in'                       => '查看类型参数错误!',
        'method.require'                => '处理类型不能为空!',
    ];

    protected $scene = [
        'create'    =>  ['name','tasklist','finish_date','important','brand_id','product_id','charge_userid','charge_departmentId','filelist'],
        'info'      =>  ['type','tid'],
        'handle'    =>  ['method','tid'],
        'delete'    =>  ['tid'],
    ];
    
    /**
     * 验证需求清单信息
     */
    protected function checkTasklist($value,$rule,$data)
    {
        if (!is_array($value)) {
            return '需求清单参数格式错误!';
        }
        if (hasDuplicates($value, 'type')) {
            return '需求清单类型参数重复!';
        }
        foreach ($value as $k => $v) {
            if (!isset($v['type'])) {
                return '需求清单类型参数不能为空!';
            }
            if (!isset($v['total'])) {
                return '需求清单数量参数不能为空!';
            }
            if (!is_numeric($v['total'])) {
                return '需求清单数量参数错误!';
            }
        }
        return true;
    }

    /**
     * 验证附件内容信息
     */
    protected function checkFilelist($value,$rule,$data)
    {
        if (!empty($value)) {
            if (!is_array($value)) {
                return '附件内容参数格式错误!';
            }
            foreach ($value as $k=>$v) {
                if (!isset($v['name'])) {
                    return '附件名称参数不能为空!';
                }
                if (!isset($v['size'])) {
                    return '附件大小参数不能为空!';
                }
                if (!isset($v['format'])) {
                    return '附件格式参数不能为空!';
                }
                if (!isset($v['url'])) {
                    return '附件地址参数不能为空!';
                }
                if (!isValidUrlRegex($v['url'])) {
                    return '附件地址参数 '. $v['url'] . ' 格式不正确!';
                }
            }
        }
        return true;
    }

    /**
     * 不同处理情况下的参数验证
     */
    protected function checkMethodParams($value,$rule,$data)
    {
        $methods = array_keys(Enum::METHOD);
        if (!in_array($value,$methods)) {
            return '处理类型参数错误!';
        }
        switch ($value) {
            case 'ACCEPT':
                return true;
            case 'REJECT':
                if (empty($data['reason'])) {
                    return '拒绝理由不能为空!';
                }    
                break;
            case 'ROLLBACK':
                if (empty($data['reason'])) {
                    return '回退理由不能为空!';
                }
                break;
            case 'CONFIRM':
                return true;
            case 'RELAUNCH':
                return true;
            case 'EDITINFO':
                if (empty($data['name'])) {
                    return '需求名称不能为空!';
                }
                if (empty($data['tasklist'])) {
                    return '需求清单不能为空!';
                }
                if (!is_array($data['tasklist'])) {
                    return '需求清单参数格式错误!';
                }
                foreach ($data['tasklist'] as $k => $v) {
                    if (!isset($v['type'])) {
                        return '需求清单类型参数不能为空!';
                    }
                    if (!isset($v['total'])) {
                        return '需求清单数量参数不能为空!';
                    }
                    if (!is_numeric($v['total'])) {
                        return '需求清单数量参数错误!';
                    }
                }
                if (empty($data['finish_date'])) {
                    return '期望日期不能为空!';
                }
                if (!isset($data['important'])) {
                    return '优先级不能为空!';
                }
                if (!in_array($data['important'],[0,1,2,3])) {
                    return '优先级参数错误!';
                }
                if (empty($data['brand_id'])) {
                    return '品牌ID不能为空!';
                }
                if (!is_numeric($data['brand_id'])) {
                    return '品牌ID参数错误!';
                }
                if (empty($data['product_id'])) {
                    return '产品ID不能为空!';
                }
                if (!is_numeric($data['product_id'])) {
                    return '产品ID参数错误!';
                }
                if (empty($data['charge_userid'])) {
                    return '负责人不能为空!';
                }
                if (empty($data['charge_departmentId'])) {
                    return '负责人部门ID不能为空!';
                }
                if (!empty($data['filelist'])) {
                    if (!is_array($data['filelist'])) {
                        return '附件内容参数格式错误!';
                    }
                    foreach ($data['filelist'] as $k=>$v) {
                        if (!isset($v['name'])) {
                            return '附件名称参数不能为空!';
                        }
                        if (!isset($v['size'])) {
                            return '附件大小参数不能为空!';
                        }
                        if (!isset($v['format'])) {
                            return '附件格式参数不能为空!';
                        }
                        if (!isset($v['url'])) {
                            return '附件地址参数不能为空!';
                        }
                        if (!isValidUrlRegex($v['url'])) {
                            return '附件地址参数 '. $v['url'] . ' 格式不正确!';
                        }
                    }    
                }
                break;
            default:
                return '处理类型参数错误!';
                break;
        }
        return true;
    }
}